Python socket.error:[Errno 104] 对方重置连接

您所在的位置:网站首页 websocket server python Python socket.error:[Errno 104] 对方重置连接

Python socket.error:[Errno 104] 对方重置连接

2023-03-20 19:59| 来源: 网络整理| 查看: 265

我有一个代码,有13个客户必须连接到服务器。然后服务器对客户提供的数据进行一些计算。在这之后,角色就转过来了--服务器变成了客户,客户变成了服务器来接收数据。 问题是,当试图进行第一次连接时,也就是13个客户端试图连接到服务器时,我一直得到这个错误。 [Errno 104] Connection reset by peer。我尝试了一些变通方法,例如在一秒钟内尝试连接5次,但没有任何效果。

Here my code:

server.py import socket, pickle, numpy as np import struct import math while 1: HOST = '' PORT = 50007 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(13) adresses = [] ports = [] i = 0 print("receiving...") while i < 13: i += 1 #wait to accept a connection - blocking call conn, addr = s.accept() print ('Connected with ', addr) adresses.append(addr[0]) buf = b'' while len(buf) < 4: buf += conn.recv(4 - len(buf)) length = struct.unpack('>I', buf)[0] data = b'' l = length while l > 0: d = conn.recv(l) l -= len(d) data += d if not data: break M = np.loads(data) if i == 1: L = M[0] else: L += M[0] ports.append(M[1]) conn.close() s.close() L /= 993040 packet = pickle.dumps(L) length = struct.pack('>I', len(packet)) packet = length + packet print("sending...") for kl, addr in enumerate(adresses): HOST = addr PORT = 50007 + ports[kl] s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) s.sendall(packet) s.close() client.py def connection(centers, kl): HOST = "192.168.143.XX" PORT = 50007 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(3600) try: s.connect((HOST, PORT)) # HERE IS AN ERROR s.settimeout(None) packet = pickle.dumps([centers, kl]) ## ??? length = struct.pack('>I', len(packet)) packet = length + packet s.sendall(packet) # OR HERE IS AN ERROR s.close() except Exception as e: print(e) print('error ', kl) s.close() return np.zeros(centers.shape) HOST = '' PORT = 50007 + kl s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(2) i = 0 while i < 1: #wait to accept a connection - blocking call conn, addr = s.accept() i += 1 print ('Connected with ', addr) buf = b'' while len(buf) < 4: buf += conn.recv(4 - len(buf)) length = struct.unpack('>I', buf)[0] data = b'' l = length while l > 0: d = conn.recv(l) l -= len(d) data += d if not data: break new_centers = np.loads(data) conn.close() s.close() return new_centers aa = 0 for k in range(99): print(k) centers = some_function(centers) time.sleep(60) centers1 = connection(centers, i) aa = 0 while not (centers1.any()) and aa < 5: time.sleep(1) centers1 = connection(centers, i) aa += 1 centers = centers1

问题是所有的13个客户端都必须连接到服务器上,否则就不会进行下一次迭代。 我正在使用Python 3.4。 请帮助我。

更新:

我已经添加了线程,但错误仍然存在。

[Errno 104] Connection reset by peer

server.py import socket, pickle, numpy as np import struct import math from multiprocessing.pool import ThreadPool def clientthread(conn, L): buf = b'' while len(buf) < 4: buf += conn.recv(4 - len(buf)) length = struct.unpack('>I', buf)[0] data = b'' l = length while l > 0: d = conn.recv(l) l -= len(d) data += d M = np.loads(data) return(M) j = 0 while 1: HOST = '' PORT = 50007 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(2) #print('0') adresses = [] ports = [] i = 0 print("receiving...") while i < 13: i += 1 #wait to accept a connection - blocking call conn, addr = s.accept() print ('Connected with ', addr) adresses.append(addr[0]) pool = ThreadPool(processes=13) async_result = pool.apply_async(clientthread, (conn, i,)) M = async_result.get() conn.close() if i == 1: L = M[0] else: L += M[0] ports.append(M[1]) s.close() L /= 993040 packet = pickle.dumps(L) length = struct.pack('>I', len(packet)) packet = length + packet for kl, addr in enumerate(adresses): HOST = addr PORT = 50007 + ports[kl] s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) s.sendall(packet) s.close()


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3